package com.example.servicelogin.controller;

import com.example.entity.utils.AliOSSUtils;
import com.example.entity.vo.ResponseVO;
import com.example.login.po.UserEasy;
import com.example.servicelogin.services.impl.TestserciceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Controller
@RequestMapping("/api/login/test")
public class TestController extends ABaseController {
    @Autowired
    private TestserciceImpl testsercice;

    @RequestMapping("test111")
    public ResponseVO test111() {
        UserEasy userEasy = new UserEasy();
        userEasy.setId(1);
        userEasy.setUserName("admin");
        userEasy.setUserPassword("123456");
        userEasy.setNickName("管理员");
        userEasy.setAddStatus(1);
        userEasy.setUserStatus(1);
        return getSuccessResponseVO(userEasy);
    }

    @PostMapping("/imageUp")
    public ResponseVO imageUp(MultipartFile multipartFile) throws IOException {
//        图片直接存入 阿里 oss
        AliOSSUtils aliOSSUtils = new AliOSSUtils();
        String upload = aliOSSUtils.upload(multipartFile);

        return getSuccessResponseVO(upload);
    }

    @RequestMapping("/queryAll")
    public ResponseVO queryAll() {
        return getSuccessResponseVO(testsercice.queryAll());
    }
    @RequestMapping("/queryByUserId")
    public ResponseVO queryByUserId(String userId) {
        return getSuccessResponseVO(testsercice.queryByUserId(userId));
    }
    @RequestMapping("/insert")
    public ResponseVO insert(String userId, String userName,
                             String password,String nickName,
                             int addStatus, int userStatus) {
        // 校验用户是否存在
        if (testsercice.queryByUserId(userId) != null) {
            return getBusinessErrorResponseVO("用户已存在");
        }
        UserEasy userEasy = new UserEasy();
        userEasy.setUserId(userId);
        userEasy.setUserName(userName);
        userEasy.setUserPassword(password);
        userEasy.setNickName(nickName);
        userEasy.setAddStatus(addStatus);
        userEasy.setUserStatus(userStatus);
        return getSuccessResponseVO(testsercice.insert(userEasy));
    }
    @RequestMapping("/update")
    public ResponseVO update(String userId, String userName,
                             String password,String nickName,
                             int addStatus, int userStatus) {
        UserEasy userEasy = new UserEasy();
        userEasy.setUserId(userId);
        userEasy.setUserName(userName);
        userEasy.setUserPassword(password);
        userEasy.setNickName(nickName);
        userEasy.setAddStatus(addStatus);
        userEasy.setUserStatus(userStatus);
        return getSuccessResponseVO(testsercice.update(userEasy));
    }
    @RequestMapping("/delete")
    public ResponseVO delete(int id) {
        return getSuccessResponseVO(testsercice.delete(id));
    }


//    空指针异常
    @GetMapping("data")
    public String test() {
        String name = null;
        name.toString();
        return "ok";
    }

    @GetMapping("data0")
    public String test11() {
        String name = null;
        return "ok";
    }


    //    算术异常
    @GetMapping("data1")
    public String test1() {
        int i = 1/0;
        return "ok";
    }


}
